﻿using DataProcess.Interface;
using DataProcess.Interface.Models;
using System;
using System.Text.RegularExpressions;

namespace DataProcessing.OXFORD_XSupreme
{

    public class DataProcessing : IDataProcessing
    {
        public string Name { get { return "OXFORD(牛津) X-Supreme"; } }

        public IDataOutput Processing(string content)
        {
            XRayOutputModel outputModel = new XRayOutputModel();
            var regContext = new Regex("[\r\n]+").Replace(content, "\r");
            string[] spdata = regContext.Split('\r');
            for (int i = 0; i <= spdata.Length - 1; i++)
            {
                switch (i)
                {
                    case 0://时间 eg:19Oct2022,08:34:45
                        var dateStr = spdata[i].TrimStart(',').TrimEnd(',');

                        if (DateTime.TryParse(dateStr, out DateTime date))
                        {
                            outputModel.MeasureTime = date;
                        }
                        else
                        {
                            outputModel.MeasureTime = DateTime.Now;
                        }
                        break;
                    case 1://样本名称和测量方法   eg:样品:  ,Method:22熟料,,,,,,,,

                        var tempArr = spdata[i].TrimStart(',').TrimEnd(',').Split(',');
                        if (tempArr.Length != 2) break;

                        var sampleArr = tempArr[0].Split(':');
                        if (sampleArr.Length == 2)
                        {
                            outputModel.SampleName = sampleArr[1];
                        }
                        else
                        {
                            outputModel.SampleName = DateTime.Now.ToString("yyyyMMddHHmm");
                        }

                        var appArr = tempArr[1].Split(':');
                        if (appArr.Length == 2)
                        {
                            outputModel.AnalyzeApplicationName = appArr[1];
                        }
                        break;
                    case 2: //说明，直接跳过  eg:,分析物,浓度,,,,,,,
                        break;
                    default:
                        var eleArr = spdata[i].TrimStart(',').TrimEnd(',').Split(',');
                        if (eleArr.Length < 2) break;
                        var eleValueArr = eleArr[1].Split(' ');
                        if (double.TryParse(eleValueArr[0], out _) && !string.IsNullOrEmpty(eleArr[0]))
                        {
                            outputModel.Eelements.Add(eleArr[0], eleValueArr[0]);
                        }
                        break;
                }
            }
            return outputModel;
        }
    }
}
